package com.sinfonia.wipes.controller.taobao;

import com.sinfonia.wipes.common.utils.R;
import com.sinfonia.wipes.common.utils.RequestUtils;
import com.sinfonia.wipes.service.taobao.SearchItemService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * Created by gmwang on 2018/10/17
 */
@Controller
@RequestMapping("/tb/search")
@Api(value = "tbSearchItem", description = "淘宝检索 API")
public class SearchItemController {
    @Autowired
    SearchItemService searchItemService;
    /**
     * 跳转页面
     * Created by gmwang on 2018/10/17
     */
    @RequestMapping("/index")
    @RequiresPermissions("tb:search:searchItem")
    public String index(HttpServletRequest request,String keyWord,String pageNum,String pageSize) {
        //获取关键词
        return "taobao/search/index";
    }
    /**
     * 根据关键词获取链接
     * Created by gmwang on 2018/10/17
     */
    @ResponseBody
    @RequestMapping("/searchItem")
    @RequiresPermissions("tb:search:searchItem")
    public R searchItem(HttpServletRequest request,String keyWord,String pageNum,String pageSize) {
        //获取关键词
        Map<String, Object> params = RequestUtils.getParameterMap(request);
        List list = null;
        try {
            list = searchItemService.searchItem(params);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return R.ok().page(list,list.size());
    }
    /**
     * 根据关键词获取联想词
     * Created by gmwang on 2018/10/17
     */
    @ResponseBody
    @RequestMapping(value = "/associativeWord")
    @RequiresPermissions("tb:search:associativeWord")
    public R associativeWord(HttpServletRequest request,String keyWord) {
        //获取关键词
        Map<String, Object> params = RequestUtils.getParameterMap(request);
        List list = null;
        try {
            list = searchItemService.associativeWord(params);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok().page(list,list.size());
    }

    /**
     * 根据类别获取热搜
     * @param request
     * @param keyWord
     * Created by kyn on 2018/10/18
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/hotWord")
    //@RequiresPermissions("tb:search:hotWord")
    public R hotWord(HttpServletRequest request,String keyWord) {
        //获取关键词
        Map<String, Object> params = RequestUtils.getParameterMap(request);
        List list = null;
        try {
            list = searchItemService.hotWord(params);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok().page(list,list.size());
    }


}
